package com.ruoyi.keep.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.score.domain.Score;
import com.ruoyi.score.service.IScoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分Controller
 * 
 * @author ruoyi
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/score/score")
public class ScoreController extends BaseController
{
    @Autowired
    private IScoreService scoreService;

    /**
     * 查询积分列表
     */
    @PreAuthorize("@ss.hasPermi('score:score:list')")
    @GetMapping("/list")
    public TableDataInfo list(Score score)
    {
        startPage();
        List<Score> list = scoreService.selectScoreList(score);
        return getDataTable(list);
    }

    /**
     * 导出积分列表
     */
    @PreAuthorize("@ss.hasPermi('score:score:export')")
    @Log(title = "积分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Score score)
    {
        List<Score> list = scoreService.selectScoreList(score);
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        util.exportExcel(response, list, "积分数据");
    }

    /**
     * 获取积分详细信息
     */
    @PreAuthorize("@ss.hasPermi('score:score:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scoreService.selectScoreById(id));
    }

    /**
     * 是否签到
     */
    @PreAuthorize("@ss.hasPermi('score:score:query')")
    @GetMapping(value = "/sign/{userId}")
    public AjaxResult isSign(@PathVariable("userId") Long userId)
    {
        return success(scoreService.selectIsSign(userId));
    }

    /**
     * 用户积分获取新增和 修改
     * userId:用户id
     * note：提示
     * score：变化积分+-
     * flag：0是其他，1是签到
     *
     */
    @PreAuthorize("@ss.hasPermi('score:score:edit')")
    @PostMapping(value = "/updateSc")
    public AjaxResult signIn(@RequestBody Map<String,Object> map)
    {
        Long userId = ((Integer)map.get("userId")).longValue();
        String note = (String)map.get("note");
        Long score = ((Integer)map.get("score")).longValue();
        Integer flag = (Integer)map.get("flag");
        return success(scoreService.signIn(userId,note,score,flag));
    }

    /**
     * 根据用户id获取积分详细信息
     * flag为1时候获取的是积分信息
     * flag不为1时候获取的带有积分信息和积分的详细信息
     */
    @PreAuthorize("@ss.hasPermi('score:score:query')")
        @GetMapping(value = "userId/{userId}/{flag}")
    public AjaxResult getInfoByUserId(@PathVariable("userId") Long userId,@PathVariable("flag") Integer flag)
    {
        if(flag!=1){
            return success(scoreService.selectScoreByUserId(userId));
        }else{
            QueryWrapper<Score> wrapper = new QueryWrapper();
            wrapper.eq("user_id",userId);
            return success(scoreService.getOne(wrapper));
        }
    }

    /**
     * 新增积分
     */
    @PreAuthorize("@ss.hasPermi('score:score:add')")
    @Log(title = "积分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Score score)
    {
        return toAjax(scoreService.insertScore(score));
    }

    /**
     * 修改积分
     */
    @PreAuthorize("@ss.hasPermi('score:score:edit')")
    @Log(title = "积分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Score score)
    {
        return toAjax(scoreService.updateScore(score));
    }

    /**
     * 删除积分
     */
    @PreAuthorize("@ss.hasPermi('score:score:remove')")
    @Log(title = "积分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scoreService.deleteScoreByIds(ids));
    }
}
